
exports.GAPersistedObject = Core.Class.subclass({
	
	initialize: function(resourceName, callback, path) {
		this._callback = callback;
		this._resourceName = resourceName;
		this.load(resourceName, path);
	},
	
	load: function(resourceName, path) {
		var pathToResource = GAConfigurations.getGameStateDBPath() + "/" +GAConfigurations.getRecordID()+"/"+ resourceName + ".json";
		//var pathToResource = GAConfigurations.getGameStateDBPath() +"/"+ resourceName + ".json";
		if ((path != null) && (path != undefined)){
			pathToResource = path + resourceName + ".json";
		}
		
		Storage.FileSystem.readFile(pathToResource, false, this.loadComplete.bind(this));
	},
	
	loadComplete: function(error, contents) {
		if (error) {
			console.log("ERROR loading the object: " + error)
			this._callback(this._resourceName,true);
		}
		else {
			var obj = eval("(" + contents + ")");
			if (!obj.classname) 
			{
				throw "Property 'classname' missing";
			}
			for (var prop in obj) 
			{
				this[prop] = obj[prop];
			}
			this._callback(this.classname);
		}
	},
	
	toJSON: function() {
		var obj = {};
		for (var prop in this) {
			if (prop != "_delegate") {
				obj[prop] = this[prop];	
			}
		}
		return obj;
	},
	
	persist: function() {
		//if(!GAConfigurations.getNeighborVisitMode())
		{
			var pathToResource = GAConfigurations.getGameStateDBPath() +"/"+GAConfigurations.getRecordID()+"/" + this.classname + ".json";
			//var pathToResource = GAConfigurations.getGameStateDBPath() +"/"+ this.classname + ".json";
			
			var jsonStr = JSON.stringify(this);
			
			Storage.FileSystem.writeFile(pathToResource, jsonStr);
		}
	},
	
	destroy: function() {
		this._callback = null;
	}
	
});